#!/bin/bash
##### 业务模块自定义配置区 开始 ##### 
APP_NAME=cmclink-document-server
JAR_NAME=cmclink-document-biz.jar

BASE_DIR=/data/webapp/$APP_NAME
DEPLOY_DIR=$BASE_DIR/deploy
LOG_DIR=$BASE_DIR/logs
NACOS_LOG_DIR=$LOG_DIR/nacos
MQ_LOG_DIR=$LOG_DIR/rocketmq
FULL_JAR_NAME=$DEPLOY_DIR/$JAR_NAME
DEPLOY_ENV=$2

JVM_OPTS="-Xms1g -Xmx1g"
# 生产环境加大内存
if [ "$DEPLOY_ENV" == "prd" ]; then
  JVM_OPTS="-Xms8g -Xmx12g"
fi

APP_JVM_CONFIG="${JVM_OPTS} -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+HeapDumpOnOutOfMemoryError -verbose:gc -Xloggc:$LOG_DIR/gc.log -XX:HeapDumpPath=$LOG_DIR -DJM.LOG.PATH=$NACOS_LOG_DIR -DJM.SNAPSHOT.PATH=$NACOS_LOG_DIR/config -DLOCAL.SNAPSHOT.PATH=$NACOS_LOG_DIR/config -Dcom.alibaba.nacos.naming.cache.dir=$NACOS_LOG_DIR/naming -Drocketmq.client.logRoot=${MQ_LOG_DIR} -Drocketmq.client.logLevel=error -Drocketmq.client.logFileMaxIndex=20"
##### 业务模块自定义配置区 结束 ##### 

source /etc/profile
if [ "x$JAVA_HOME" = "x" ]; then
   JAVA_CMD="/data/jdk1.8.0_432/bin/java"
else 
   JAVA_CMD="$JAVA_HOME/bin/java"
fi
echo "JAVA_CMD is $JAVA_CMD"

#使用说明，用来提示输入参数
usage() {
    echo "Usage: sh start.sh [start|stop|restart|status]"
    exit 1
}

#检查程序是否在运行
is_exist() { 
    pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
    #如果不存在返回1，存在返回0
    if [ -z "${pid}" ]; then
      return 1
    else
      return 0
    fi
}

#启动方法
start() {
   is_exist
   if [ $? -eq "0" ]; then
     echo "${APP_NAME} is running. pid=${pid} we will stop it frist."
	 stop
   fi
   echo "start to run ${APP_NAME}"
   
   nohup $JAVA_CMD -server -javaagent:/data/apm/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name="cmclink::cmclink-document" -Dskywalking.collector.backend_service="$APM_SERVICE" $APP_JVM_CONFIG -jar $FULL_JAR_NAME --spring.config.location=${DEPLOY_DIR}/  > /dev/null 2>&1 &
   #echo "$JAVA_CMD -server -javaagent:/data/apm/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name="cmclink::cmclink-document" -Dskywalking.collector.backend_service="$APM_SERVICE" $APP_JVM_CONFIG -jar $FULL_JAR_NAME --spring.config.location=${DEPLOY_DIR}/ "
}

#停止方法
stop() {
   
   is_exist
   k=$?
   count=0
   if [ $k -eq 0 ]; then
     echo "${APP_NAME} $pid is stoped"
     for id in $pid
       do
         kill $id
         while [ $k -eq 0 ]; do
			# rocketmq client有时无法正常关闭，原因未知，暂定10秒之后还未停止就强行停止，避免部署错误
		    count=$((count + 1))
		    if [ $count -eq 11 ]; then
		        echo "kill timeout. force kill"
				kill -9 $id
				k=1
		    else
                sleep 1
                echo "${APP_NAME} is stoping... $id"
                is_exist
                k=$?
		    fi
         done
      done
    else
      echo "${APP_NAME} is not running"
    fi
}

#输出运行状态
status() {
   is_exist
   if [ $? -eq "0" ]; then
     echo "${APP_NAME} is running. Pid is ${pid}"
   else
     echo "${APP_NAME} is not running."
   fi
}

#重启
restart() {
   stop
   start
}

#根据输入参数，选择执行对应方法，不输入则执行使用说明
case "$1" in
   "start")
     start
     ;;
   "stop")
     stop
     ;;
   "status")
     status
     ;;
   "restart")
     restart
     ;;
   *)
     usage
     ;;
esac
